
import { _decorator, Component, Node, Button, EditBox } from 'cc';
import { Constants } from '../configs/Constants';
import { CustomEventListener } from '../data/CustomEventListener';
import { E2LProtocol, L2EProtocol } from '../proto/ELProtocol';
import { Utils } from '../utils/utils';
const { ccclass, property } = _decorator;
 
@ccclass('Login')
export class Login extends Component {
    @property({
        type: Node
    })
    pwBtn: Node = null;
    
    @property({
        type: Node
    })
    loginDialog: Node = null;

    @property({
        type: Node
    })
    loginPanel: Node = null;

    @property({
        type: Node
    })
    registerPanel: Node = null;

    @property({
        type: EditBox
    })
    loginAccount: EditBox = null;

    @property({
        type: EditBox
    })
    loginPassword: EditBox = null;

    @property({
        type: EditBox
    })
    regAccount: EditBox = null;

    @property({
        type: EditBox
    })
    regPassword: EditBox = null;

    onLoad() {
        // CustomEventListener.dispatchEvent(E2LProtocol.e2l_start_login);
    }

    start () {
        this.pwBtn.active = false;
        this.loginDialog.active = false;
        // CustomEventListener.dispatchEvent(E2LProtocol.e2l_start_login);
    }

    // update (deltaTime: number) {
    //     // [4]
    // }

    clickPWBtn() {
        this.loginDialog.active = true;
        this.loginPanel.active = true;
        this.registerPanel.active = false;
    }

    clickCloseDialog() {
        this.loginDialog.active = false;
    }

    clickLogin() {
        this.loginPassword.string = "123456";
        let accountStr = this.loginAccount.string;
        if (Utils.api_check_phone_valid(accountStr) === false) {
            CustomEventListener.dispatchEvent(L2EProtocol.l2e_open_msg_box, {
                "type": Constants.BOX_BTN_TYPE.know,
                "text": "手机号只能是11位数字!",
                "fairy": 1
            })
            return;
        }

        CustomEventListener.dispatchEvent(L2EProtocol.l2e_login_with_pw, accountStr, this.loginPassword.string);
    }

    clickShowReg() {
        this.loginPanel.active = false;
        this.registerPanel.active = true;
    }

    clickReg() {
        this.regPassword.string = "123456";
        var accountStr = this.regAccount.string;
        if (Utils.api_check_phone_valid(accountStr) === false) {
            CustomEventListener.dispatchEvent(L2EProtocol.l2e_open_msg_box, {
                "type": Constants.BOX_BTN_TYPE.know,
                "text": "手机号只能是11位数字!",
                "fairy": 1
            })
            return;
        }

        CustomEventListener.dispatchEvent(L2EProtocol.l2e_reg_with_pw, accountStr, this.regPassword.string, "888888");
    }

    public showLoginButton() {
        this.pwBtn.active = true;
        this.loginDialog.active = false;
    }
}
